package com.lzx.applib.utils;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.lzx.applib.R;

/**
 * Created by Lzx on 16/12/21 0021.
 */
public class PopWindowUtil {
    public static final String TAG = "PopWindowUtil";

    public static PopupWindow showPopBottomBar(final Activity activity, View contentView) {
        PopupWindow popupWindow = getPopupWindow(activity, contentView);
        popupWindow.setAnimationStyle(R.style.pop_bottom_bar_anim_style);
        popupWindow.showAtLocation(activity.findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
        return popupWindow;
    }

    public static PopupWindow showPopTopBar(final Activity activity, View contentView) {
        PopupWindow popupWindow = getPopupWindow(activity, contentView);
        popupWindow.setAnimationStyle(R.style.pop_top_bar_anim_style);
        popupWindow.showAtLocation(activity.findViewById(android.R.id.content), Gravity.TOP, 0, 0);
        return popupWindow;
    }

    public static PopupWindow getPopupWindow(final Activity activity, View contentView) {
        final PopupWindow popupWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (popupWindow.isShowing()) {
                            popupWindow.dismiss();
                        }
                    }
                    return true;
                }
                return false;
            }
        });
        addAnimationToPopupWindow(popupWindow, activity);

        return popupWindow;
    }

    public static void addAnimationToPopupWindow(PopupWindow popupWindow, final Activity activity) {

        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                ValueAnimator animator = ValueAnimator.ofFloat(activity.getWindow().getAttributes().alpha, 1f);
                animator.setDuration(activity.getResources().getInteger(R.integer.anim_pop_duration));
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                        lp.alpha = (float) animation.getAnimatedValue();
                        activity.getWindow().setAttributes(lp);
                    }
                });
                animator.start();
            }
        });

        ValueAnimator animator = ValueAnimator.ofFloat(activity.getWindow().getAttributes().alpha, 0.7f);
        animator.setDuration(activity.getResources().getInteger(R.integer.anim_pop_duration));
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
                lp.alpha = (float) animation.getAnimatedValue();
                activity.getWindow().setAttributes(lp);
            }
        });
        animator.start();
    }
}
